package com.youdu.service.notification;

import android.accessibilityservice.AccessibilityService;
import android.app.Notification;
import android.app.PendingIntent;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.accessibility.AccessibilityEvent;
import android.widget.RemoteViews;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Anyway on 2017/2/11.
 */

public class WeChatAccessibilityService extends AccessibilityService {

  @Override public void onAccessibilityEvent(AccessibilityEvent event) {
    if (Build.VERSION.SDK_INT < 18) {
      Notification notification = (Notification) event.getParcelableData();
      List<String> textList = getText(notification);
      if (textList != null && textList.size() > 0) {
        for (String text : textList) {
          if (!TextUtils.isEmpty(text) && text.contains("[微信红包]")) {
            final PendingIntent pendingIntent = notification.contentIntent;
            try {
              if (pendingIntent != null) {
                pendingIntent.send();
              }
            } catch (PendingIntent.CanceledException e) {
              e.printStackTrace();
            }
          }
          break;
        }
      }
    }
  }

  @Override public void onInterrupt() {

  }

  public List<String> getText(Notification notification) {
    if (null == notification) {
      return null;
    }
    RemoteViews views = notification.bigContentView;
    if (views == null) {
      views = notification.contentView;
    }
    if (views == null) {
      return null;
    }
    // Use reflection to examine the m_actions member of the given RemoteViews object.
    // It's not pretty, but it works.
    List<String> text = new ArrayList<>();
    try {
      Field field = views.getClass().getDeclaredField("mActions");
      field.setAccessible(true);
      @SuppressWarnings("unchecked") ArrayList<Parcelable> actions =
          (ArrayList<Parcelable>) field.get(views);
      // Find the setText() and setTime() reflection actions
      for (Parcelable p : actions) {
        Parcel parcel = Parcel.obtain();
        p.writeToParcel(parcel, 0);
        parcel.setDataPosition(0);
        // The tag tells which type of action it is (2 is ReflectionAction, from the source)
        int tag = parcel.readInt();
        if (tag != 2) continue;
        // View ID
        parcel.readInt();
        String methodName = parcel.readString();
        if (null == methodName) {
          continue;
        } else if (methodName.equals("setText")) {
          // Parameter type (10 = Character Sequence)
          parcel.readInt();
          // Store the actual string
          String t = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel).toString().trim();
          text.add(t);
        }
        parcel.recycle();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return text;
  }
}
